﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using WWDemon.Core;
using WWDemon.Model;

namespace WWDemon.Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            this.Load += Form1_Load;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GlobalSubPub.Instance.Subscribe<CommondPC>(Model.CommondKey.Message, (s) =>
            {
                var msg = s.Data.ToObjectByte<MessageInfo>();

                MessageBox.Show(msg.ErrorMes);
            });

            GlobalSubPub.Instance.Subscribe<string>(Model.CommondKey.Connect, (s) =>
            {
                log_txt.AppendText("服务器连接成功" + Environment.NewLine);
                buttonLink.Enabled = false;
            });
            GlobalSubPub.Instance.Subscribe<string>(Model.CommondKey.Close, (s) =>
            {
                buttonLogin.Enabled = true;
                buttonLink.Enabled = true;
                log_txt.AppendText("服务器连接断开" + Environment.NewLine);
            });
            GlobalSubPub.Instance.Subscribe<CommondPC>(Model.CommondKey.Login, (s) =>
            {
                buttonLogin.Enabled = false;
                log_txt.AppendText("登陆成功" + Environment.NewLine);
            });
            GlobalSubPub.Instance.Subscribe<CommondPC>(Model.CommondKey.GetOrderInfo, (s) =>
            {

                //这里 我客户PC 模拟  去取 订单数据  ，  将订单数据 回发给服务器

                var orderInfo = s.Data.ToObjectByte<GetOrderInfo>();
                string sendText = serContent_txt.Text;

                SendOrderInfoModel sendOrderInfo = new SendOrderInfoModel();
                sendOrderInfo.TestData = new List<TestData>();
                sendOrderInfo.TestData.Add(new TestData() { Id = 122, Name = sendText });

                sendOrderInfo.TestDateTime = DateTime.Now;
                sendOrderInfo.Testdecimal = 123m;
                sendOrderInfo.Testdouble = 456d;
                sendOrderInfo.Testint = 11;
                sendOrderInfo.Testlong = 45645648974564564l;
                sendOrderInfo.TestString = sendText + orderInfo.OrderId;

                //我随便写的订单数据，到时你自己改


                ClientServer.Instance.SendData(CommondKey.GetOrderInfo, sendOrderInfo);
                //这里 我把数据回复给服务器

                MessageBox.Show("我收到采集命令了");

                //整个流程就是
                // 管理员 在服务器 发了一个命令 给PC客户端， PC客户端收到X命令，做X业务，然后回发一个消息给服务器 ，告诉服务器X命令我处理完了，这里是结果。
                // 然后服务器显示 结果 

                //刚刚不就是显示  web 界面 发送一个命令 ，客户端响应，  服务器显示结果嘛 
            });
        }

        private async void buttonLink_Click(object sender, EventArgs e)
        {
            await ClientServer.Instance.Init(url_txt.Text, Convert.ToInt32(prot_txt.Text));
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            ClientServer.Instance.SendData(CommondKey.Login, new ClientLoginModel() { AccountName = user_txt.Text });
        }
    }
}